package com.shm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.CommentLike;
import com.ruoyi.common.core.domain.entity.ProductLike;
import com.ruoyi.common.core.domain.entity.Star;
import com.shm.service.ICommentLikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论——点赞Controller
 *
 * @author dam
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/market/commentLike")
@Api
public class CommentLikeController extends BaseController {
    @Autowired
    private ICommentLikeService commentLikeService;

    /**
     * 查询评论——点赞列表
     */
    @PreAuthorize("@ss.hasPermi('market:commentLike:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommentLike commentLike) {
        startPage();
        List<CommentLike> list = commentLikeService.list(new QueryWrapper<CommentLike>(commentLike));
        return getDataTable(list);
    }

    /**
     * 导出评论——点赞列表
     */
    @PreAuthorize("@ss.hasPermi('market:commentLike:export')")
    @Log(title = "评论——点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommentLike commentLike) {
        List<CommentLike> list = commentLikeService.list(new QueryWrapper<CommentLike>(commentLike));
        ExcelUtil<CommentLike> util = new ExcelUtil<CommentLike>(CommentLike.class);
        util.exportExcel(response, list, "评论——点赞数据");
    }

    /**
     * 获取评论——点赞详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:commentLike:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(commentLikeService.getById(id));
    }

    /**
     * 新增评论——点赞
     */
    @PreAuthorize("@ss.hasPermi('market:commentLike:add')")
    @Log(title = "评论——点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommentLike commentLike) {
        return toAjax(commentLikeService.save(commentLike));
    }

    /**
     * 修改评论——点赞
     */
    @PreAuthorize("@ss.hasPermi('market:commentLike:edit')")
    @Log(title = "评论——点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommentLike commentLike) {
        return toAjax(commentLikeService.updateById(commentLike));
    }

    /**
     * 删除评论——点赞
     */
    @PreAuthorize("@ss.hasPermi('market:commentLike:remove')")
    @Log(title = "评论——点赞", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(commentLikeService.removeByIds(ids));
    }

    /**
     * 点赞评论
     */
    @PreAuthorize("@ss.hasPermi('market:commentLike:like')")
    @GetMapping("/likeComment/{commentId}")
    @ApiOperation("点赞评论")
    public AjaxResult likeComment(@PathVariable("commentId") Long commentId) {
        CommentLike commentLike = new CommentLike();
        commentLike.setUserId(getLoginUser().getUserId());
        commentLike.setCommentId(commentId);
        boolean save = commentLikeService.save(commentLike);
        return AjaxResult.success();
    }

    /**
     * 取消点赞评论
     */
    @PreAuthorize("@ss.hasPermi('market:commentLike:remove')")
    @Log(title = "取消关注", businessType = BusinessType.DELETE)
    @GetMapping("/cancelLikeComment/{commentId}")
    public AjaxResult cancelLikeComment(@PathVariable("commentId") Long commentId) {
        QueryWrapper<CommentLike> queryWrapper = new QueryWrapper<CommentLike>();
        queryWrapper.eq("user_id", getLoginUser().getUserId());
        queryWrapper.eq("comment_id", commentId);
        boolean remove = commentLikeService.remove(queryWrapper);
        return AjaxResult.success();
    }
}
